﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.TemplateManagement;
using System.Resources;
using System.Reflection;
using KOPLibrary.PreferenceManagement;
using System.IO;

namespace KOPLibrary.Forms
{
    public partial class TemplateBrowserForm : Form
    {
        public TemplateType Type { get; private set; }
        public string Directory { get; private set; }
        private string filepath;
        public TemplateBrowserForm()
        {
            InitializeComponent();

        }

        public string Execute()
        {
            InitUI();
            DialogResult result = ShowDialog();
            if (result != DialogResult.OK)
                return string.Empty;
            else
            {
                return filepath;
            }
        }

        private void InitUI()
        {
            ResourceManager rmanager = new ResourceManager("KOPLibrary.ImageResource", Assembly.GetExecutingAssembly());
            if (Type == TemplateType.Word)
            {
                ctlPictureBox.Image = (Image)rmanager.GetObject("Word");
                openFileDialog1.Filter = "Word Belgeleri ve Şablonları|*.docx;*.dotx";
            }
            else
            {
                ctlPictureBox.Image = (Image)rmanager.GetObject("Excel");
                openFileDialog1.Filter = "Excel Belgeleri|*.xlsx;*.xltx";
            }
            openFileDialog1.InitialDirectory = PreferenceManager.ReadKey("DefaultTemplatePath", Type);
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
        }

        public static TemplateBrowserForm Create(TemplateType type, string initialDirectory)
        {
            TemplateBrowserForm form = new TemplateBrowserForm();
            form.Type = type;
            form.Directory = initialDirectory;
            return form;
        }

        private void TemplateBrowserForm_Load(object sender, EventArgs e)
        {
            List<string> openedemplates = PreferenceManager.GetGroupValues(@"RecentTemplates\" + Type.ToString());            
            int typeid;
            foreach (string item in openedemplates)
            {
                if (!File.Exists(item)) continue;
                typeid = (Type == TemplateType.Word) ? 0 : 1;
                ListViewItem newitem = new ListViewItem(Path.GetFileNameWithoutExtension(item), typeid);
                newitem.ToolTipText = item;
                ctlListView.Items.Add(newitem);
            }

        }

        private void ctlOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ctlpathlabel.Text = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
                filepath = openFileDialog1.FileName;
                DialogResult = DialogResult.OK;
            }
        }

        private void ctlCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void ctlListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ctlListView.SelectedItems.Count > 0)
            {
                ctlpathlabel.Text =ctlListView.SelectedItems[0].Text;
                ctlpathlabel.ToolTipText = ctlListView.SelectedItems[0].ToolTipText;                
            }
        }

        private void ctlListView_DoubleClick(object sender, EventArgs e)
        {
            if (ctlListView.SelectedItems.Count > 0)
            {
                filepath = ctlListView.SelectedItems[0].ToolTipText;
                DialogResult = DialogResult.OK;
            }
        }

        
    }
}
